package com.film.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.film.dao.PerformerDao;
import com.film.domain.Film;
import com.film.domain.Performer;
import com.film.rowMapper.FilmRowMapper;
import com.film.rowMapper.PerformerRowMapper;
import com.film.util.DBUtil;

public class PerformerDaoImpl implements PerformerDao {
	private JdbcTemplate temp = new JdbcTemplate(DBUtil.getDataScorse());
	@Override
	public List<Performer> selectFilmPerformer(int film_id) {
		String sql = "select * from performer t1,film_performer t2 where t1.per_id = t2.per_id and t2.film_id = ?";
		return temp.query(sql, new PerformerRowMapper(), film_id);
	}
	@Override
	public Performer selectOnePerformer(int perId) {
		String sql = "select * from performer where per_id = ?";
		List<Performer> performers = temp.query(sql, new PerformerRowMapper(),perId);
		if(performers !=null) {
			return performers.get(0);
		}else {
			return null;
		}
	}
	@Override
	public List<Film> perIdSelectFilm(int perId) {	
		String sql = "select distinct t1.* from film t1 , film_performer t2 ,performer t3 where t1.film_id = t2.film_id and t2.per_id = ?";
		return temp.query(sql,new FilmRowMapper(),perId);
	}
	@Override
	public List<Performer> selectAllPerformer() {
		String sql = "select * from performer";
		return temp.query(sql, new PerformerRowMapper());
	}
	@Override
	public int addFilmPerformer(int filmId, int perId) {
		String sql = "insert into film_performer values(default,?,?)";
		return temp.update(sql,filmId,perId);

	}



















	@Override
	public int addPerformer(Performer performer) {
		String sql = "insert into performer(per_id,per_name,per_foreignName,per_nickname,per_birth,per_height,per_sex,per_nationality,per_birthPlace,per_star,per_introduce,per_img)"
				+ "values (default,?,?,?,?,?,?,?,?,?,?,?)";
		return temp.update(sql,performer.getPerName(),performer.getPerNickName(),performer.getPerTypr(),performer.getPerBirth(),performer.getPerHeight(),performer.getPerSex(),performer.getPerNationnality(),performer.getPerBirthPlace(),performer.getPerStar(),performer.getPerIntroduce(),performer.getPerImg());
	}
	@Override
	public int deletePerformer(int per_id) {
		String sql = "delete from performer where per_id = ?";
		return temp.update(sql,per_id);
	}
	@Override
	public List<Performer> selectPerformer() {
		String sql = "select * from performer";
		return temp.query(sql,new PerformerRowMapper());
	}
	@Override
	public int updatePerformer(Performer performer) {
		String sql = "update performer set per_name = ?,per_foreignName = ?,per_nickname = ?,per_birth = ?,per_height = ?,per_sex = ?,per_nationality = ?,per_birthPlace = ?,per_star = ?,per_introduce = ?,per_img = ?where per_id = ?";
		return temp.update(sql,performer.getPerName(),performer.getPerNickName(),performer.getPerTypr(),performer.getPerBirth(),performer.getPerHeight(),performer.getPerSex(),performer.getPerNationnality(),performer.getPerBirthPlace(),performer.getPerStar(),performer.getPerIntroduce(),performer.getPerImg(),performer.getPerId());
	}
	@Override
	public List<Performer> selectPerformer(int perId) {
		String sql = "select * from performer where per_id = ?";
		return temp.query(sql, new PerformerRowMapper(),perId);
	}
	@Override
	public List<Performer> selectPerformer(int firstPage, int selectPageNum) {
		String sql = "select * from performer limit ?,?";
		return temp.query(sql, new PerformerRowMapper(),firstPage,selectPageNum);
	}
	

}
